package com.inbox.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.inbox.domain.Message;
import com.inbox.util.HibernateUtils;

/**
 * 收件箱处理类
 * 
 * @author Wangpeihu
 * @since 08/01/2010
 * @version 1.0
 */
public class InboxService {

	/** 默认排序 */
	public static final int ORDER_DEFAULT = 1;

	/** 按标题升序 */
	public static final int ORDER_TITLE_ASC = 2;

	/** 按标题降序 */
	public static final int ORDER_TITLE_DESC = 3;

	/** 按发件时间升序 */
	public static final int ORDER_TIME_ASC = 4;

	/** 按发件时间降序 */
	public static final int ORDER_TIME_DESC = 5;

	/** 按发件人升序 */
	public static final int ORDER__FROM_ASC = 6;

	/** 按发件人降序 */
	public static final int ORDER_FROM_DESC = 7;

	/** 存放各种排序查询语句 */
	private Map<Integer, String> querys = new HashMap<Integer, String>();

	/**
	 * 创建该类的时候，就将排序条件加入到类属性中
	 */
	public InboxService() {
		this.initQuerys();
	}

	/**
	 * 排序条件
	 * 
	 * @return
	 */
	public Map<Integer, String> getMessageOrderOptions() {
		Map<Integer, String> options = new HashMap<Integer, String>();
		options.put(ORDER_DEFAULT, "默认排序");
		options.put(ORDER_TITLE_ASC, "按标题升序");
		options.put(ORDER_TITLE_DESC, "按标题降序");
		options.put(ORDER_TIME_ASC, "按发件时间升序");
		options.put(ORDER_TIME_DESC, "按发件时间降序");
		options.put(ORDER__FROM_ASC, "按发件人升序");
		options.put(ORDER_FROM_DESC, "按发件人降序");
		return options;
	}

	/**
	 * 排序查询语句
	 */
	private void initQuerys() {
		String sql = "from Message msg join fetch msg.fromId join fetch msg.toId ";
		querys.put(ORDER_DEFAULT, sql);
		querys.put(ORDER_TITLE_ASC, sql + "order by msg.title asc");
		querys.put(ORDER_TITLE_DESC, sql + "order by msg.title desc");
		querys.put(ORDER_TIME_ASC, sql + "order by msg.time asc");
		querys.put(ORDER_TIME_DESC, sql + "order by msg.time desc");
		querys.put(ORDER__FROM_ASC, sql + "order by msg.fromId asc");
		querys.put(ORDER_FROM_DESC, sql + "order by msg.fromId desc");
	}

	/**
	 * * 取得所有的信息 该方法根据指定的条件得到结果<br>
	 * 
	 * @param rowsPerPage
	 * @param page
	 * @param order
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Message> findAllMessage(int rowsPerPage, int page, int order) {
		Session session = HibernateUtils.getSession();

		/** 联合抓取，将发件人和收件人一道取出 */
		Query query = session.createQuery(querys.get(order));

		/** 从第几行开始取 */
		query.setFirstResult((page - 1) * rowsPerPage);

		/** 要取多少行 */
		query.setMaxResults(rowsPerPage);
		List<Message> list = query.list();
		session.close();
		return list;
	}

	/**
	 * 取得所有的信息 该方法根据指定的条件得到结果<br>
	 * 
	 * @param rowsPerPage
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Message> findAllMessage(int rowsPerPage, int page) {
		Session session = HibernateUtils.getSession();
		String hql = "from Message msg join fetch msg.fromId join fetch msg.toId";
		Query query = session.createQuery(hql);
		query.setFirstResult((page - 1) * rowsPerPage);
		query.setMaxResults(rowsPerPage);
		List<Message> list = query.list();
		session.close();
		return list;
	}

	/**
	 * 根据指定的页数计算每页显示多少行
	 * 
	 * @param pages
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int getMessageTotalPages(int rowsPerPage) {
		Session session = HibernateUtils.getSession();
		Query query = session.createQuery("select count(*) from Message");
		List countMsg = query.list();
		session.close();
		/**
		 * 由于Hibernate版本原因，这里可能得到的结果是Long类型的<br>
		 * 我们用Number的intValue()方法取，就没有问题<br>
		 * 在这里我们要强调的是包装类的父类都Number这个类<br>
		 */
		int totalRows = ((Number) countMsg.get(0)).intValue();
		int totalPages;
		if (totalRows % rowsPerPage == 0) {
			totalPages = totalRows / rowsPerPage;
		} else {
			/** 如果有余的,那这个余数一定小于除数,这里加1就可以将剩余的条目加在最后一页 */
			totalPages = totalRows / rowsPerPage + 1;
		}
		return totalPages;
	}

	/**
	 * 添加一条信息
	 * 
	 * @param message
	 */
	public void insertMessage(Message message) {
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(message);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public static void main(String[] args) {
		InboxService is = new InboxService();
		int list = is.getMessageTotalPages(5);
		System.out.println(list);
	}
}
